﻿#ifndef BLUETOOTHDEV_H
#define BLUETOOTHDEV_H

#include <QMainWindow>
#include <QListWidgetItem>
#include <QBluetoothSocket>
#include <QBluetoothLocalDevice>
#include <QBluetoothDeviceInfo>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothServiceInfo>
#include <QMessageBox>
#include <QDateTime>
#include <QTimer>
#include <QInputDialog>
#include <QShowEvent>
#include <QLabel>
#include <QButtonGroup>

QT_BEGIN_NAMESPACE
namespace Ui { class BluetoothDev; }
QT_END_NAMESPACE

class BluetoothDev : public QMainWindow
{
    Q_OBJECT

public:
    BluetoothDev(QWidget *parent = nullptr);
    ~BluetoothDev();
    void statusBarLog(const QString& info);
protected:
    void showEvent(QShowEvent*) override;
private slots:
    void newDeviceFinded(const QBluetoothDeviceInfo &info);
    void DiscoveryAgentError(QBluetoothDeviceDiscoveryAgent::Error error);

    void socketStateChange(QBluetoothSocket::SocketState state);
    void socketMsg();

    void pairFinish(const QBluetoothAddress &address, QBluetoothLocalDevice::Pairing pairing);
    void localDevError(QBluetoothLocalDevice::Error error);

    void on_manageSocket_clicked();

    void on_sendMsg_clicked();

    void on_actSearchBT_triggered();

    void on_actStopSearch_triggered();

    void on_actCloseBT_triggered();

    void on_actHexDisp_triggered();

    void on_actHexSend_triggered();

    void on_actAsciiDisp_triggered();

    void on_actAsciiSend_triggered();

    void on_actReciveDelay_triggered();

    void on_searchSocket_clicked();

    void on_rb_sendAscii_clicked();

    void on_rb_sendHex_clicked();

    void on_rb_reciveAscii_clicked();

    void on_rb_reciveHex_clicked();

    void on_pbt_clearRecive_clicked();

    void on_pbt_clearCount_clicked();

    void on_pbt_clearSend_clicked();

    void on_actOpenBT_triggered();

    void on_actLocalCharSet_triggered();

    void on_actUTF8CharSet_triggered();

private:
    Ui::BluetoothDev *ui;

    enum dispmode{d_ascii, d_hex};
    enum sendmode{s_ascii, s_hex};
    enum charSet{useLocal, useUtf8};

    QTimer* timer;
    const QString serviceUuid = "00001101-0000-1000-8000-00805F9B34FB";
    QBluetoothLocalDevice* BDevice = nullptr;
    QBluetoothSocket* BSocket = nullptr;
    QBluetoothDeviceDiscoveryAgent* BAgent = nullptr;

    QMessageBox MessageBox;

    QLabel* statusBarLable = nullptr;
    QButtonGroup sendGroup;
    QButtonGroup reciveGroup;

    int dispmode = d_ascii;
    int sendmode = s_ascii;
    int useCharSet = useLocal;
    int sendCount = 0;
    int reciveCount = 0;
    int ReciveDelay = 100;

};
#endif // BLUETOOTHDEV_H
